﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace Data.Service.Service
{
    public static class BaseServer<T> where T : class ,new()
    {
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public static IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda)
        {
            SYTDataBaseEntities en = new SYTDataBaseEntities();

            return en.Set<T>().Where<T>(whereLambda);
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool AddEntity(T entity)
        {
            SYTDataBaseEntities en = new Model.SYTDataBaseEntities();
            en.Set<T>().Add(entity);
            return en.SaveChanges() > 0;
        }


        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool DelEntity(T entity)
        {
            SYTDataBaseEntities en = new SYTDataBaseEntities();
            en.Entry<T>(entity).State = System.Data.EntityState.Deleted;
            return en.SaveChanges() > 0;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static bool UpdateEntity(T entity)
        {
            SYTDataBaseEntities en = new SYTDataBaseEntities();
            en.Entry<T>(entity).State = System.Data.EntityState.Modified;
            return en.SaveChanges() > 0;

        }
        /// <summary>
        /// 1.分页
        /// </summary>
        /// <typeparam name="s"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderLambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public static IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderLambda, bool isAsc)
        {
            SYTDataBaseEntities en = new SYTDataBaseEntities();
            var temp = en.Set<T>().Where<T>(whereLambda);
            totalCount = temp.Count();
            if (isAsc)//表示升序
            {
                temp = temp.OrderBy<T, s>(orderLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            else
            {
                temp = temp.OrderByDescending<T, s>(orderLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            return temp;
        }
        /// <summary>
        /// 2.分页
        /// </summary>
        /// <typeparam name="s"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderLambda"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public static IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderLambda, bool isAsc)
        {
            SYTDataBaseEntities en = new SYTDataBaseEntities();
            var temp = en.Set<T>().Where<T>(whereLambda);
            if (isAsc)//表示升序
            {
                temp = temp.OrderBy<T, s>(orderLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            else
            {
                temp = temp.OrderByDescending<T, s>(orderLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);
            }
            return temp;
        }
    }

}
